package com.example.practice.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.practice.R;
import com.example.practice.utils.SharedpreferencesUtils;

public class findPasswordActivity extends AppCompatActivity {
    private TextView tv_title;
    private TextView tv_back;
    private EditText et_userName_yl;
    private EditText et_realName_yl;
    private TextView tv_initPwd_yl;
    private String securityName_yl;
    private String userName_yl,realName_yl,initPwd_yl;
    private Button verify_btn_yl;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_password);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        userName_yl=SharedpreferencesUtils.readLoginUserName(this);

        initView();

    }

    private void initView() {
        tv_title=findViewById(R.id.tv_title);
        tv_title.setText("找回密码");
        tv_back=findViewById(R.id.tv_back);
        et_userName_yl=findViewById(R.id.et_userName_yl);
        et_realName_yl=findViewById(R.id.et_realName_yl);
        tv_initPwd_yl=findViewById(R.id.tv_initPwd_yl);
        verify_btn_yl=findViewById(R.id.btn_verify_yl);

        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                findPasswordActivity.this.finish();
            }
        });

        verify_btn_yl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                verify();
            }
        });


    }

    //验证功能
    private void verify(){
        userName_yl=et_userName_yl.getText().toString().trim();
        realName_yl=et_realName_yl.getText().toString().trim();
        securityName_yl=getSecurity(userName_yl);

        if (TextUtils.isEmpty(userName_yl)){
            Toast.makeText(findPasswordActivity.this,"用户名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        else if(!isExistUserName(userName_yl)){
            Toast.makeText(findPasswordActivity.this,"用户名不存在",Toast.LENGTH_SHORT).show();
            return;
        }
        else if (TextUtils.isEmpty(realName_yl)){
            Toast.makeText(findPasswordActivity.this,"密保姓名不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        else if (!realName_yl.equals(securityName_yl)){
            Toast.makeText(findPasswordActivity.this,"密保姓名不正确",Toast.LENGTH_SHORT).show();
            return;
        }
        else {
            tv_initPwd_yl.setVisibility(View.VISIBLE);
            tv_initPwd_yl.setText("初始密码:123456");
            setInitPassword(userName_yl);
            Intent data=new Intent();
            data.putExtra("userName", userName_yl);
            setResult(RESULT_OK,data);
            findPasswordActivity.this.finish();
        }

    }

    //获取密保姓名
    private String getSecurity(String userName){
        SharedPreferences sp= getSharedPreferences("loginInfo",MODE_PRIVATE);
        securityName_yl= sp.getString(userName+"_security","");
        return securityName_yl;
    }

    //设置用户初始密码为123456
    private void setInitPassword(String userName){
        SharedPreferences sp=getSharedPreferences("loginInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putString(userName,"123456");
        editor.commit();
    }

    //判断用户名是否存在
    private boolean isExistUserName(String userName){
        boolean hasUserName=false;
        SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
        String spUserName=sp.getString(userName,"");
        if(!TextUtils.isEmpty(spUserName)){
            hasUserName=true;
        }
        return hasUserName;
    }
}